#include "registerwidget.h"
#include "ui_registerwidget.h"
#include "logonwidget.h"
#include "user.cpp"
#include "user.h"
//#include "mainChat.cpp"
#include<vector>
#include<QRadioButton>
extern vector<user> vec;
extern vector<user>:: iterator it;
RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);
    connect(ui->btn_cancel,&QPushButton::clicked,this, &RegisterWidget::on_btn_cancel_clicked);
    connect(ui->btn_register,&QPushButton::clicked,this, &RegisterWidget::on_btn_register_clicked);
    //connect(ui->buttonGroup, SIGNAL(toggled(bool)), this, SLOT(on_btn_Toggled(bool)));
}

RegisterWidget::~RegisterWidget()
{
    delete ui;
}
void RegisterWidget::on_btn_register_clicked()
{
    //get register information
    QString regUsername = ui->reg_username_lineEdit->text();
    QString regPassword = ui->reg_password_lineEdit->text();
    QString regPasswordConfirm = ui->reg_password_conf_lineEdit->text();

    if(regPassword == regPasswordConfirm)
    {
        //connect to server
        QNetworkRequest request(QUrl("http://localhost:19999/slimarveous/register"));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QJsonObject json;
        json.insert("username", regUsername);
        json.insert("password", regPassword);

        QJsonDocument doc(json);
        QByteArray data = doc.toJson(QJsonDocument::Compact);
        QNetworkReply *reply = manager.post(request, data);
        connect(reply, &QNetworkReply::finished, [=]()
        {
            if (reply->error() == QNetworkReply::NoError) {
                    // 读取响应数据
                    QByteArray dataReply = reply->readAll();

                    // 将 JSON 数据转换为 QVariant 对象
                    QJsonParseError error;
                    QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
                    // 处理 JSON 数据
                    if (error.error == QJsonParseError::NoError) {
                        QVariantMap resultMap = result.toVariantMap();
                        //提取状态码
                        QVariant status_code = resultMap.value("status_code");
                        status_code.toString();
                        //判断注册是否有效
                        //注册有效
                        //QMap userMap=resultMap.value("user").toMap();
                        QString id=resultMap.value("id").toString();
                        if(status_code == "2"){
                            QMessageBox::information(this,"success","恭喜你，注册成功！");
                         /*   user tmp(id,regUsername,regPassword);

                            cout<<tmp.getUserGender()<<endl;;
                            vec.push_back(tmp);

                            for(it=vec.begin();it!=vec.end();it++)
                                cout<<"该用户id为: "<<((*it).getUserId()).toStdString()<<endl;
*/
                            this->close();
                            LogOnWidget *w2=new LogOnWidget();
                            //MainInterfaceWidget* w1 = new MainInterfaceWidget();
                            w2->show();
                        }
                        //冲突的用户名
                        else if(status_code == "1") {
                            QMessageBox::information(this,"failed","该用户名已被使用！");
                        }
                        //服务器出问题
                        else {
                            QMessageBox::information(this,"failed","好像哪里出了问题呢？肯定不是服务器的问题！");
                        }

                    }

            }
            else {
                qDebug()<<"error";
            }

                // 释放资源
                reply->deleteLater();

        });
    }
    else if(regPassword != regPasswordConfirm) {
        QMessageBox::information(this,"failed","两次输入的密码不一致！");
    }
}


void RegisterWidget::on_btn_return_clicked()
{
    this->close();
    LogOnWidget* w1 = new LogOnWidget();
    w1->show();
}

void RegisterWidget::on_btn_cancel_clicked()
{
    //this->close();
    ui->reg_username_lineEdit->clear();
    ui->reg_password_lineEdit->clear();
    ui->reg_password_conf_lineEdit->clear();
    //ui->radio_gender_female->;
   // ui->radio_gender_male->;
   /* RegisterWidget* w1 = new RegisterWidget();
    w1->show();*/
}
